package de.lmu.mvs.lerngruppenfinder.utils;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;

import android.app.Application;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.os.Build;
import android.support.v4.util.LruCache;
import android.text.TextUtils;
import android.view.MenuItem;

/**
 * Centralized place for application-wide fields and services
 * 
 * Credit:
 * http://arnab.ch/blog/2013/08/asynchronous-http-requests-in-android-using
 * -volley/
 * 
 * @author chris
 * 
 */
public class ApplicationController extends Application {

	public static final String TAG = ApplicationController.class.getName();

	/**
	 * Global request queue for Volley
	 */
	private RequestQueue mRequestQueue;

	private ImageLoader mImageLoader;
	
	private static Typeface font;

	/**
	 * A singleton instance of the application class for easy access in other
	 * places
	 */
	private static ApplicationController sInstance;

	@Override
	public void onCreate() {
		super.onCreate();

		font = Typeface.createFromAsset( getAssets(), "fontawesome-webfont.ttf" );

		// initialize the singleton
		sInstance = this;
	}
	
	/**
	 * @return ApplicationController singleton instance
	 */
	public static synchronized ApplicationController getInstance() {
		return sInstance;
	}

	/**
	 * @return The Volley Request queue, the queue will be created if it is null
	 */
	public RequestQueue getRequestQueue() {
		// lazy initialize the request queue, the queue instance will be
		// created when it is accessed for the first time
		if (mRequestQueue == null) {
			mRequestQueue = Volley.newRequestQueue(getApplicationContext());
		}

		return mRequestQueue;
	}

	/**
	 * @return Volley ImageLoader Object for automatic image loading and caching
	 */
	public ImageLoader getImageLoader(){
		if(mImageLoader == null){
			mImageLoader = new ImageLoader(this.mRequestQueue, new ImageLoader.ImageCache() {
				private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>(10);
				public void putBitmap(String url, Bitmap bitmap) {
					mCache.put(url, bitmap);
				}
				public Bitmap getBitmap(String url) {
					return mCache.get(url);
				}
			});
		}
		
		return mImageLoader;
	}

	/**
	 * Adds the specified request to the global queue, if tag is specified then
	 * it is used else Default TAG is used.
	 * 
	 * @param req
	 * @param tag
	 */
	public <T> void addToRequestQueue(Request<T> req, String tag) {
		// set the default tag if tag is empty
		req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);

		VolleyLog.d("Adding request to queue: %s", req.getUrl());

		getRequestQueue().add(req);
	}

	/**
	 * Adds the specified request to the global queue using the Default TAG.
	 * 
	 * @param req
	 * @param tag
	 */
	public <T> void addToRequestQueue(Request<T> req) {
		// set the default tag if tag is empty
		req.setTag(TAG);

		getRequestQueue().add(req);
	}

	/**
	 * Cancels all pending requests by the specified TAG, it is important to
	 * specify a TAG so that the pending/ongoing requests can be cancelled.
	 * 
	 * @param tag
	 */
	public void cancelPendingRequests(Object tag) {
		if (mRequestQueue != null) {
			mRequestQueue.cancelAll(tag);
		}
	}

	public static int getScreenWidth(){
		return getInstance().getResources().getDisplayMetrics().widthPixels;
	}

	public static int getScreenHeight(){
		return getInstance().getResources().getDisplayMetrics().heightPixels;
	}
	
	public static Typeface getTypeFace(){
		return font;
	}
}
